package com.datastructure.queue;

import com.sun.deploy.util.StringUtils;

/**
 *@author:wzj
 *@Date:2019/11/17 13:05
 *@EML:15934369823@163.com
 *@explain:数据结构:队列使用数组实现  会造成浪费
*/
public class ArrayQueue {
    public static void main(String[] args) {
        Queue queue = new Queue(5);
        queue.addData(1);
        queue.addData(2);
        queue.addData(3);
        int remove = queue.remove();
        System.out.println("remove = " + remove);
        int i = queue.headQueue();
        System.out.println(i);
    }
}
class Queue{
    private int maxSize; //队列的大小
    private int front;   //指向队列头
    private int rear;    //指向队列尾部
    private int[] arr;   //存放的数据

    public Queue(int maxSize){
        this.maxSize=maxSize;
        front = -1;
        rear = -1;
        arr=new int[maxSize];
    }
    //判断队列是否为空
    public boolean isEmpty(){
        return front==rear;
    }
    //判断队列是否已满
    public boolean isFull(){
        return rear==maxSize-1;
    }
    //添加数据
    public void addData(int data){
        if(isFull()){
            throw new RuntimeException("队列已满,不能再添加");
        }
        rear++;
        arr[rear]=data;
    }
    //移除数据
    public int remove(){
        if (isEmpty()){
            throw new RuntimeException("队列为空");
        }
        front++;
        return arr[front];
    }
    //查看队列的全部数据
    public void showQueue(){
        for (int i = 0; i < arr.length; i++) {
            System.out.printf("%d\t",arr[i]);
        }
    }
    //获取头数据
    public int headQueue(){
        if(isEmpty()){
            throw new RuntimeException("队列为空");
        }
        //front+1的原因 : 从-1开始的,假如取出一个数据,指向的是一个空的所以要+1
        return arr[front+1];
    }
}
